﻿//正则 验证
var validate = new Validate();
var url = new Url();
var $Id = url.GetQueryString("id");
var serializer = new JSONSerializer();
var dict = new Dict();
var step = [0, 0, 0, 0];
//更新进度条
function updateProgress() {
    var total = 0;
    for (var i = 0; i < step.length; i++) {
        total += step[i];
    }
    $(".progressBar").text(total + "%").css("width", total + "%");
}
$(function () {
    //绑定共同居住者
    var getCommonOccup = dict.GetDictDDLByType(5);
    $(".commonOccupants").html(getCommonOccup);
    //绑定最高学历
    var edu = dict.GetDictDDLByType(2);
    $(".education").html(edu);
    //绑定公司类型
    var companyType = dict.GetDictDDLByType(6);
    $(".companyType").html(companyType);
    //绑定房产情况
    var houseProperty = dict.GetDictDDLByType(4);
    $(".houseProperty").html(houseProperty);

    //绑定婚姻状况    
    var maritalStatus = dict.GetDictRadioListByType(3, 'MaritalStatus');
    $(".maritalStatus").append(maritalStatus);

    //绑定籍贯省份状况
    var nativePlaceProvince = dict.GetDictDDLByTypePId(7, 0);
    $(".nativePlaceProvince").html(nativePlaceProvince);
    //绑定籍贯市
    $(".nativePlaceProvince").change(function () {
        var pid = $(this).val();
        var nativePlaceCity = dict.GetDictDDLByTypePId(7, pid);
        $(".nativePlaceCity").html(nativePlaceCity);
    });

    //绑定户口所在地省份状况
    var regPlaceProvince = dict.GetDictDDLByTypePId(7, 0);
    $(".regPlaceProvince").html(regPlaceProvince);
    //绑定户口所在地市
    $(".regPlaceProvince").change(function () {
        var pid = $(this).val();
        var regPlaceCity = dict.GetDictDDLByTypePId(7, pid);
        $(".regPlaceCity").html(regPlaceCity);
    });


    //绑定工作城市所在地省份状况
    var workProvince = dict.GetDictDDLByTypePId(7, 0);
    $(".workProvince").html(workProvince);
    //绑定工作城市所在地市
    $(".workProvince").change(function () {
        var pid = $(this).val();
        var workCity = dict.GetDictDDLByTypePId(7, pid);
        $(".workCity").html(workCity);
    })

    if ($Id) {//编辑模式，获取并绑定用户信息
        $.getJSON("../../Handler/LoanUserInfo.ashx",
            { Id: $Id, Type: 4, _: Math.random() },
            function (result) {
                if (result.Status) {
                    $(".nativePlaceProvince").val(dict.GetDictById(result.Data.NativePlace).Data.ParentId);
                    $(".nativePlaceProvince").change();
                    $(".regPlaceProvince").val(dict.GetDictById(result.Data.RegPlace).Data.ParentId);
                    $(".regPlaceProvince").change();
                    serializer.DeSerialize($("#dataArea"), result.Data);
                    var birthDate = getLocalTime(result.Data.BirthDate);
                    $("#txtBirthDate").val(birthDate);
                    $("#dtp_BirthDate").val(birthDate);
                    $(".male")[0].checked = result.Data.Sex;
                    $(".female")[0].checked = !result.Data.Sex;
                    $(".hdUserId").val(result.Data.UserId);
                    step[0] = 25;
                    $(".ul1 li:contains('家庭信息')").click();
                    $(".ul1 li:contains('工作信息')").click();
                }
                else {
                    alert(result.Message);
                }
            });
    }
})
//获取本地时间
function getLocalTime(nS) {
    var date = new Date(parseInt(/\d+/.exec(nS)));
    var result = date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();
    return result;
}

//保存个人信息
$(".SavePersonal").click(function () {
    var errorNode = null;
    $("#dataArea .text").each(function () {
        if (!$(this).val() || $(this).val() == -1) {
            $name = $(this).attr("data-name");
            errorNode = $(this);
            $(this).next("span").text($name + "不能为空！");
            $(this).focus();
            return false;
        }
        else {
            if ($(this).attr("class").indexOf("verifyPhone") > 0) {
                if (!validate.CheckPhone($(this).val())) {
                    $name = $(this).attr("data-name");
                    errorNode = $(this);
                    $(this).next("span").text($name + "无效的手机号码！");
                    $(this).focus();
                    return false;
                }
            }
        }
    });
    if (errorNode) {
        return;
    }
    var msg = new Array("验证通过", "非法身份证号", "非法地区", "非法生日");
    var i = validate.CheckIdCard($(".idCard").val());
    if (i < 0) {
        $(".idCard").focus();
        $(".errorIdCard").text(msg[Math.abs(i)]);
        return;
    }
    var $formData = serializer.Serialize($("#dataArea")[0]);
    $formData.type = $Id ? 2 : 1;
    if ($Id) {
        $formData.Id = $Id;
    }
    $formData._ = Math.random();
    $formData.Sex = $(".male")[0].checked;
    $.ajax({
        url: "../../Handler/LoanUserInfo.ashx",
        type: "post",
        dataType: "json",
        data: $formData,
        async: true,
        cache: true,
        complete: function () { },
        success:function (result) {
                alert(result.Message);
                if (result.Status) {
                    $(".hdUserId").val(result.Data);
                    step[0] = 25;
                    updateProgress();
                    $(".ul1 li:contains('工作信息')").click();
                }
            }
    });
});

//$(".SavePersonal").click(function () {
//    document.getElementById('dataArea').style.display = "none";
//    document.getElementById('workInfoDate').style.display = "inline";
//});

//当控件中值变化是判断是否为空
$("#dataArea .text").change(function () {
    if (!$(this).val() || $(this).val() == -1) {
        var $name = $(this).attr("data-name");
        errorNode = $(this);
        $(this).next("span").text($name + "不能为空！");
        $(this).focus();
        return false;
    }
    else {
        if ($(this).attr("class").indexOf("numFix") > 0) {
            //数字验证
            if (!validate.CheckNumber($(this).val())) {
                $(this).focus();
                var $name = $(this).attr("data-name");
                $(this).next("span").text("请输入正确的数字格式 .例如：100,100.11"); //判断只能输入 大于0的整数或小数 正则表达式
                return;
            }
            else {
                $(this).next("span").empty();
            }
        }
        else {
            $(this).next("span").empty();
        }
    }
});

$(".idCard").change(function () {

    $(".errorIdCard").text("");
    //身份证验证
    var msg = new Array("验证通过", "非法身份证号", "非法地区", "非法生日");
    var i = validate.CheckIdCard($(".idCard").val());
    if (i < 0) {
        $(".idCard").focus();
        $(".errorIdCard").text(msg[Math.abs(i)]);
        return;
    }
    else {
        var birthDate = birthDateByIdCard($(this).val());
        var maleOrFemal = maleOrFemalByIdCard($(this).val());
        var province = provinceByIdCard($(this).val());
        $.ajax({
            type: "post",
            url: "../Handler/LoanUserInfo.ashx",
            dataType: "json",
            data: { Type: 9, IDCard: $(this).val(), _: Math.random() },
            async: true,
            cache: false,
            success: function (result) {
                if (result.Status) {
                    if (result.Count == 0) {
                        alert(result.Message);
                        location.href = "/View/LoanUserInfo.aspx?css=1017&id=" + result.Data.UserId;
                    }
                    else {
                        $(".txtBirthDate").val(birthDate);
                        $(".dtp_BirthDate").val(birthDate);
                        $(".male")[0].checked = maleOrFemal;
                        $(".female")[0].checked = !maleOrFemal;
                        $(".nativePlaceProvince").val(province);
                        $(".nativePlaceProvince").change();
                        $(".regPlaceProvince").val(province);
                        $(".regPlaceProvince").change();
                        $(".workProvince").val(province);
                        $(".workProvince").change();
                    }
                }
                else {
                    if (result.Count == 0) {
                        alert(result.Message);
                        location.href = "/View/Backlog.aspx?css=1030";
                    }
                    //else
                    //{
                    //    alert(result.Message);
                    //    location.href = "/View/Backlog.aspx?css=1030";
                    //}

                }
            }
        });



    }
});


/**  
 * 通过身份证判断是男是女  
 * @param idCard 15/18位身份证号码   
 * @return 'female'-女、'male'-男  
 */
function maleOrFemalByIdCard(idCard) {
    if (idCard.length == 15) {
        if (idCard.substr(14, 1) % 2 == 0) {
            return false;
        } else {
            return true;
        }
    } else if (idCard.length == 18) {
        if (idCard.substr(16, 1) % 2 == 0) {
            return false;
        } else {
            return true;
        }
    } else {
        return null;
    }
}
/*
*通过身份证判断出生日期 1 3 0 5 2 1 9 2 1 2 1 8 8 2 6  
*/
function birthDateByIdCard(idCard) {
    if (idCard.length == 15) {
        return '19' + idCard.substring(6, 8) + '/' + idCard.substring(8, 10) + '/' + idCard.substring(10, 12);
    } else if (idCard.length == 18) {
        return idCard.substring(6, 10) + '/' + idCard.substring(10, 12) + '/' + idCard.substring(12, 14);
    }
}

/* 通过身份证 判断所在省份*/
var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" };
var arrCityId = { 11: 1, 12: 2, 13: 3, 14: 4, 15: 5, 21: 6, 22: 7, 23: 8, 31: 9, 32: 10, 33: 11, 34: 12, 35: 13, 36: 14, 37: 15, 41: 16, 42: 17, 43: 18, 44: 19, 46: 20, 45: 21, 62: 22, 61: 23, 65: 24, 63: 25, 64: 26, 50: 27, 51: 28, 52: 29, 53: 30, 54: 31, 71: 32, 82: 33, 81: 34 };
function provinceByIdCard(idCard) {

    var provNo = idCard.substring(0, 2);
    var provId = arrCityId[provNo];
    return provId;

}

//绑定工作信息
$(".ul1 li:contains('工作信息')").click(function () {
    if ($Id && !$(".hdWorkId").val()) {
        $.getJSON("../../Handler/UserWorkInfoHandler.ashx",
            { UserId: $(".hdUserId").val(), type: 4, _: Math.random() },
            function (result) {
                if (result.Status) {
                    $(".workProvince").val(dict.GetDictById(result.Data.WorkCity).Data.ParentId);
                    $(".workProvince").change();
                    serializer.DeSerialize($("#workInfoDate"), result.Data);
                    var enterCompanyDate = getLocalTime(result.Data.EnterCompanyDate);
                    $("#txtEnterCompanyDate").val(enterCompanyDate);
                    $("#dtp_EnterCompanyDate").val(enterCompanyDate);
                    $(".hdWorkId").val(result.Data.WorkId);
                    step[1] = 25;
                    updateProgress();
                }
            }
        )
    }
});


//保存工作信息
$(".SaveWorkInfo").click(function () {
    if (!$(".hdUserId").val()) {
        alert("请先添加用户信息！");
        $(".ul1 li:eq(0)").click();
        return;
    }
    var errorNode = null;
    $("#workInfoDate .text").each(function () {
        if (!$(this).val() || $(this).val() == -1) {
            $name = $(this).attr("data-name");
            errorNode = $(this);
            $(this).next("span").text($name + "不能为空！");
            $(this).focus();
            return false;
        }
        else {
            if ($(this).attr("class").indexOf("verifyPhone") > 0) {
                if (!validate.CheckPhone($(this).val())) {
                    $name = $(this).attr("data-name");
                    errorNode = $(this);
                    $(this).next("span").text($name + "无效的手机号码！");
                    $(this).focus();
                    return false;
                }
            }
            if ($(this).attr("class").indexOf("Email") > 0) {
                if (!validate.CheckEmail($(this).val())) {
                    $name = $(this).attr("data-name");
                    errorNode = $(this);
                    $(this).next("span").text($name + "无效的邮箱！");
                    $(this).focus();
                    return false;
                }
            }
        }
    });

    if (errorNode) {
        return;
    }
    var serializer = new JSONSerializer();
    var $formData = serializer.Serialize($("#workInfoDate")[0]);
    $formData._ = Math.random();
    $Id = url.GetQueryString("id");
    $formData.type = $Id ? 2 : 1;
    $formData.UserId = $(".hdUserId").val();
    $.getJSON(
        "../../Handler/UserWorkInfoHandler.ashx",
        $formData,
        function (result) {
            alert(result.Message);
            if (result.Status) {
                step[1] = 25;
                updateProgress();
                $(".ul1 li:contains('家庭信息')").click();
            }
        }
    );

});

//当控件中值变化是判断是否为空
$("#workInfoDate .text").change(function () {
    if (!$(this).val() || $(this).val() == -1) {
        var $name = $(this).attr("data-name");
        errorNode = $(this);
        $(this).next("span").text($name + "不能为空！");
        $(this).focus();
        return false;
    }
    else {
        $(this).next("span").empty();
    }
});

//绑定家庭信息
$(".ul1 li:contains('家庭信息'),.ul1 li:contains('紧急联系人信息')").click(function () {
    if ($Id && !$(".hdFimailyInfoId").val()) {
        $.getJSON("../../Handler/UserFamilyInfoHandler.ashx",
            { UserId: $(".hdUserId").val(), type: 4, _: Math.random() },
            function (result) {
                if (result.Status) {
                    $(".hdFimailyInfoId").val(result.Data.FamilyId);
                    serializer.DeSerialize($("#userFimailyInfo"), result.Data);
                    $(".maritalStatus :radio[value='" + result.Data.MaritalStatus + "']")[0].checked = true;
                    serializer.DeSerialize($("#userFimailyInfo2"), result.Data);
                    //$('.yes')[0].checked = result.Data.IsHaveChild;
                    //$('.no')[0].checked = !result.Data.IsHaveChild;
                    $("input:radio[name='IsHaveChild']")[0].checked = result.Data.IsHaveChild;
                    step[2] = 25;
                    if (result.Data.EmergLinkManName1) {
                        step[3] = 25;
                    }
                    updateProgress();
                }
            }
        )
    }
});

//保存家庭信息
$(".SaveFamilyInfo1").click(function () {
    if (!$(".hdUserId").val()) {
        alert("请先添加用户信息！");
        $(".ul1 li:eq(0)").click();
        return;
    }
    var errorNode = null;
    $("#userFimailyInfo .text").each(function () {
        if (!$(this).val() || $(this).val() == -1) {
            $name = $(this).attr("data-name");
            errorNode = $(this);
            $(this).next("span").text($name + "不能为空！");
            $(this).focus();
            return false;
        }
        else {
            if ($(this).attr("class").indexOf("verifyPhone") > 0) {
                if (!validate.CheckPhone($(this).val())) {
                    $name = $(this).attr("data-name");
                    errorNode = $(this);
                    $(this).next("span").text($name + "无效的手机号码！");
                    $(this).focus();
                    return false;
                }
            }
        }
    });

    if (errorNode) {
        return;
    }
    var serializer = new JSONSerializer();
    var $formData = serializer.Serialize($("#userFimailyInfo")[0]);
    $formData.type = $Id ? 2 : 1;
    $formData._ = Math.random();
    $formData.UserId = $(".hdUserId").val();
    $formData.MaritalStatus = $('input:radio[name="MaritalStatus"]:checked').val();
    $formData.IsHaveChild = $(".yes").prop("checked");
    $.getJSON(
        "../../Handler/UserFamilyInfoHandler.ashx",
        $formData,
        function (result) {
            alert(result.Message);
            if (result.Status) {
                $(".hdFimailyInfoId").val(result.Data);
                $(".ul1 li:contains('紧急联系人信息')").click();
                step[2] = 25;
                updateProgress();
            }
        }
    );
});


//当控件中值变化是判断是否为空
$("#userFimailyInfo .text").change(function () {
    if (!$(this).val() || $(this).val() == -1) {
        var $name = $(this).attr("data-name");
        errorNode = $(this);
        $(this).next("span").text($name + "不能为空！");
        $(this).focus();
        return false;
    }
    else {
        $(this).next("span").empty();
    }
});

$(".getdate").change(function () {
    $(this).parent().next().next().empty();
})

//保存紧急联系人
$(".SaveFamilyInfo2").click(function () {
    if (!$(".hdFimailyInfoId").val()) {
        alert("请先添加家庭信息！");
        $(".ul1 li:contains('家庭信息')").click();
        return;
    }
    var errorNode = null;
    $("#userFimailyInfo2 .text").each(function () {
        if (!$(this).val() || $(this).val() == -1) {
            $name = $(this).attr("data-name");
            errorNode = $(this);
            $(this).next("span").text($name + "不能为空！");
            $(this).focus();
            return false;
        }
        else {
            if ($(this).attr("class").indexOf("verifyPhone") > 0) {
                if (!validate.CheckPhone($(this).val())) {
                    $name = $(this).attr("data-name");
                    errorNode = $(this);
                    $(this).next("span").text($name + "无效的手机号码！");
                    $(this).focus();
                    return false;
                }
            }
        }
    });

    if (errorNode) {
        return;
    }
    var serializer = new JSONSerializer();
    var $formData = serializer.Serialize($("#userFimailyInfo2")[0]);
    $formData.type = 5;
    $formData._ = Math.random();
    $formData.Id = $(".hdFimailyInfoId").val();
    $.getJSON(
        "../../Handler/UserFamilyInfoHandler.ashx",
        $formData,
        function (result) {
            console.log(result);
            if (result.Status) {
                step[3] = 25;
                updateProgress();
                location.href = $Id ? "/View/LoanUserList.aspx?css=2033" : "/View/LoanNewProj.aspx?css=1016";
            }
            alert(result.Message);
        }
    );

});

//当控件中值变化是判断是否为空
$("#userFimailyInfo2 .text").change(function () {
    if (!$(this).val() || $(this).val() == -1) {
        var $name = $(this).attr("data-name");
        errorNode = $(this);
        $(this).next("span").text($name + "不能为空！");
        $(this).focus();
        return false;
    }
    else {
        $(this).next("span").empty();
    }
});
